package com.yz.sharkim.exception;

import com.yz.sharkim.entity.enums.CommonResult;
import com.yz.sharkim.entity.message.response.CommonResponse;
import com.yz.sharkim.util.ConvertUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * @author yangzheng
 * @version 1.0
 * @date 2019/11/4
 */
public abstract class AbstractWebSocketException extends RuntimeException {

    private final int no;

    private final CommonResult result;

    public AbstractWebSocketException(int no, CommonResult result) {
        this.no = no;
        this.result = result;
    }

    public ChannelFuture sendException(ChannelHandlerContext context) {
        return context.channel().writeAndFlush(new TextWebSocketFrame(
                ConvertUtil.toJson(new CommonResponse().setResponseNo(no).setResult(result))
        ));
    }
}
